package org.foj.model {
import mx.messaging.ChannelSet;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.remoting.RemoteObject;

public class LoginModel {

  private var _userService:RemoteObject;
  private var _defaultChannelSet:ChannelSet;
  private var _jmsChannelSet:ChannelSet;

  public function LoginModel() {
    _defaultChannelSet = ChannelSetFactory.getDefaultChannel();
//    _jmsChannelSet = ChannelSetFactory.getMessagingChannel();
//
    _userService = new RemoteObject();
    _userService.destination = "userDao";
    _userService.channelSet = _defaultChannelSet;
  }

  public function login(username:String,
                        password:String,
                        responder:IResponder):void {

    if (!_defaultChannelSet.authenticated) {
      var token:AsyncToken =
          _defaultChannelSet.login(username, password);
      token.addResponder(responder);
//      _jmsChannelSet.login(username, password);
    }
  }

  public function logout(responder:IResponder):void {
    var token:AsyncToken = _defaultChannelSet.logout();
    token.addResponder(responder);
  }

  public function getUserDetails(username:String,
                                 responder:IResponder):void {
    var token:AsyncToken = _userService.loadUserByUsername(username);
    token.addResponder(responder);
  }

}
}